package cn.chen.service.impl;

import cn.chen.dao.mapper.SysUserMapper;
import cn.chen.dao.pojo.SysUser;
import cn.chen.service.LoginService;
import cn.chen.service.SysUserService;
import cn.chen.utils.JWTUtils;
import cn.chen.vo.ErrorCode;
import cn.chen.vo.LoginUserVo;
import cn.chen.vo.Result;
import cn.chen.vo.UserVo;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private LoginService loginService;
    @Autowired
    private RedisTemplate<String,String > redisTemplate;

    @Override
    public UserVo findUserVoById(Long id) {
        SysUser sysUser = sysUserMapper.selectById(id);
        //防止空指针
        if(sysUser==null){
            sysUser=new SysUser();
            sysUser.setId(1L);
            sysUser.setAvatar("/static/img/logo.b3a48c0.png");
            sysUser.setNickname("阿里嘎多多");
        }
        UserVo userVo=new UserVo();
//        userVo.setId(sysUser.getId());
//        userVo.setAvatar(sysUser.getAvatar());
//        userVo.setNickname(sysUser.getNickname());
        BeanUtils.copyProperties(sysUser,userVo);
        return userVo;
    }

    @Override
    public SysUser findUserById(Long id) {
        SysUser sysUser = sysUserMapper.selectById(id);
        //防止空指针
        if(sysUser==null){
            sysUser=new SysUser();
            sysUser.setNickname("阿里嘎多多");
        }
        return sysUser;
    }

    /**
     * 登陆的时候查询用户密码
     * @param account
     * @param password
     * @return
     */
    @Override
    public SysUser findUser(String account, String password) {
        LambdaQueryWrapper<SysUser> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getAccount,account);
        queryWrapper.eq(SysUser::getPassword,password);
        queryWrapper.select(SysUser::getAccount,SysUser::getId,SysUser::getAvatar,SysUser::getNickname);
        queryWrapper.last("limit 1");
        SysUser sysUser = sysUserMapper.selectOne(queryWrapper);
        return sysUser;
    }

    /**
     * 根据token查询用户信息
     * @param token
     * @return
     */
    @Override
    public Result findUserInfoByToken(String token) {
        /**
         * 1 token合法性校验(是否为空,解析是否成功,redis是否存在)
         * 2 失败
         * 3 成功 返回对应的结果 loginUserVo
         */
        SysUser sysUser=loginService.checkToken(token);
        if(sysUser==null){
            return Result.fail(ErrorCode.TOKEN_ERROR.getCode(), ErrorCode.TOKEN_ERROR.getMsg());
        }
        LoginUserVo loginUserVo=new LoginUserVo();
        loginUserVo.setId(sysUser.getId());
        loginUserVo.setAccount(sysUser.getAccount());
        loginUserVo.setAvatar(sysUser.getAvatar());
        loginUserVo.setNickname(sysUser.getNickname());
        return Result.success(loginUserVo);
    }

    @Override
    public SysUser findUserByAccount(String account) {
        LambdaQueryWrapper<SysUser> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getAccount,account);
        queryWrapper.last("limit 1");
        return sysUserMapper.selectOne(queryWrapper);
    }

    @Override
    public void save(SysUser sysUser) {
        //id自动生成 分布式id 雪花算法
        sysUserMapper.insert(sysUser);
    }
}
